<template>
  <el-row :gutter="20">
    <!-- 左侧部门树 -->
    <el-col :span="4" :xs="24">
      <ContentWrap class="h-1/1">
        <CommunityTree @node-click="handleCommunityNodeClick" />
      </ContentWrap>
    </el-col>
    <el-col :span="20" :xs="24">
      <!-- 搜索 -->
      <ContentWrap>
        <el-form
          class="-mb-15px"
          :model="queryParams"
          ref="queryFormRef"
          :inline="true"
          label-width="68px"
        >
          <el-form-item label="租住地址" prop="rentAddrName">
            <el-input
              v-model="queryParams.rentAddrName"
              placeholder="请输入租住地址"
              clearable
              @keyup.enter.stop="handleQuery"
            />
          </el-form-item>
          <el-form-item label="租客姓名" prop="rentUserName">
            <el-input
              v-model="queryParams.rentUserName"
              placeholder="请输入租客姓名"
              clearable
              @keyup.enter.stop="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button @click="handleQuery"><Icon icon="ep:search" />搜索</el-button>
            <el-button
              type="primary"
              plain
              @click="openForm('create')"
              v-hasPermi="['zwsj:room-rent:create']"
            >
              <Icon icon="ep:plus" /> 新增入住</el-button
            >
          </el-form-item>
        </el-form>
      </ContentWrap>
      <ContentWrap>
        <el-table v-loading="loading" :data="list">
          <el-table-column label="主键" align="center" prop="id" width="60" />
          <el-table-column label="租住地址" align="left" prop="rentAddrName">
            <template #default="scope">
              <i
                style="margin-right: 10px; font-size: 14px; color: green"
                @click="showMeter(scope.row.roomId)"
                >电表</i
              >
              <span>{{ scope.row.rentAddrName }}</span>
            </template>
          </el-table-column>
          <el-table-column label="租客姓名" align="center" prop="rentUserName" width="130" />
          <el-table-column label="租客手机号码" align="center" width="120">
            <template #default="scope">
              <span v-if="scope.row.rentUserResp">{{ scope.row.rentUserResp.mobile }}</span>
            </template>
          </el-table-column>
          <el-table-column label="登记时间" align="center" prop="createTime" width="120">
            <template #default="scope">
              <span>{{ formatDate(scope.row.createTime, 'YYYY-MM-DD') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" fixed="right" width="160">
            <template #default="scope">
              <el-button
                type="primary"
                link
                @click="openForm('update', scope.row.id)"
                v-hasPermi="['zwsj:room-rent:update']"
              >
                更换地址
              </el-button>
              <el-button
                link
                type="danger"
                @click="handleDelete(scope.row.id)"
                v-hasPermi="['zwsj:room-rent:delete']"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <Pagination
          :total="total"
          v-model:page="queryParams.pageNo"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </ContentWrap>
    </el-col>
  </el-row>

  <!-- 添加或修改房屋对话框 -->
  <RoomRentForm ref="formRef" @success="getList" />
  <MeterView ref="showMeterRef" />
</template>
<script lang="ts" setup>
import { formatDate } from '@/utils/formatTime'
import * as RoomRentApi from '@/api/zwsj/roomRent'
import RoomRentForm from './RoomRentForm.vue'
import CommunityTree from '@/views/zwsj/common/CommunityTree.vue'
import MeterView from '@/views/zwsj/meter/MeterView.vue'

defineOptions({ name: 'RoomRent' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数

const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  commId: null,
  rentAddrName: null,
  rentUserName: null
})
const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await RoomRentApi.getRoomRentPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 处理区域被点击 */
const handleCommunityNodeClick = async (row) => {
  queryParams.commId = row.id
  await getList()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await RoomRentApi.deleteRoomRent(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 显示电表 */
const showMeterRef = ref()
const showMeter = async (roomId: number) => {
  //通过房间查询出房间对应的电表ID，如果没有电表则提示
  showMeterRef.value.openByRoomId(roomId)
}

/** 初始化 */
onMounted(() => {
  getList()
})
</script>
